//
//  codeViewController.m
//  ioschishan
//
//  Created by 千翼网络科技 on 2017/7/11.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "codeViewController.h"
#import "setPwdViewController.h"

@interface codeViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UITextField *codeTextField;
@property (nonatomic,strong) UIButton *nextBtn;

@end

@implementation codeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

#pragma mark ----------------------- init ---------------------

- (void)initView{
    
    self.title = @"找回密码";
    
    _codeTextField = [[UITextField alloc]init];
    _codeTextField.placeholder = @"请输入短信验证码";
    _codeTextField.delegate = self;
    _codeTextField.keyboardType = UIKeyboardTypeNumberPad;
    _codeTextField.returnKeyType = UIReturnKeyNext;
    _codeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _codeTextField.font = get4sFont(12);
    [self.view addSubview:_codeTextField];
    [_codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(31));
        make.left.mas_equalTo(self.view).with.offset(screenScale(25));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-120));
        make.height.mas_equalTo(screenScale(15));
    }];
    
    UIView *phoneThread = [[UIView alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(62), screenScale(335), screenScale(0.5))];
    phoneThread.backgroundColor = getThreadColor();
    [self.view addSubview:phoneThread];
    
    UIButton *codeBtn = [ConfigXp buttonWithFrame:CGRectMake(screenScale(265), screenScale(27), screenScale(90), screenScale(24))
                                            title:@"获取验证码"
                                       titleColor:getColor(@"ffffff")
                                             font:get4sFont(10)
                                  backgroundColor:getMainColor()
                                            image:nil];
    codeBtn.layer.cornerRadius = screenScale(3);
    [codeBtn addTarget:self action:@selector(codeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:codeBtn];
    
    _nextBtn = [ConfigXp buttonWithFrame:CGRectMake(screenScale(20), screenScale(88), screenScale(335), screenScale(44))
                                   title:@"下一步"
                              titleColor:getColor(@"ffffff")
                                    font:get4sFont(13)
                         backgroundColor:getMainColor()
                                   image:nil];
    _nextBtn.layer.cornerRadius = screenScale(3);
    [_nextBtn addTarget:self action:@selector(nextBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_nextBtn];
}

#pragma mark ----------------------- click ---------------------

- (void)codeBtnClick{

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: _phoneStr forKey:@"phone"];
    
    [[MNetworkHandler sharedInstance]conURL:login_smsSend
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
}

- (void)nextBtnClick{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: _phoneStr forKey:@"phone"];
    [productParams setValue: _codeTextField.text forKey:@"code"];
    [productParams setValue: @"2" forKey:@"type"];
    
    [[MNetworkHandler sharedInstance]conURL:login_verifyPhoneCode
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       setPwdViewController *vc = [[setPwdViewController alloc]init];
                                       vc.phoneStr = _phoneStr;
                                       [self.navigationController pushViewController:vc animated:YES];
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
}

#pragma mark ----------------------- UITextFieldDelegate ---------------------

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == _codeTextField) {
        
        if (string.length == 0) return YES;
        
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        
        if (existedLength - selectedLength + replaceLength > 6) {
            
            return NO;
        }
    }
    
    return YES;
}

@end
